﻿using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.ServiceContracts;
using KendoGridBinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkTicket.Application.Implementation;
using WorkTicket.Website.Common;

namespace WorkTicket.Website.Controllers
{
    public class MenuController : Controller
    {
        protected IMenuService menuService = new MenuServiceImpl();
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            List<Menu> result = menuService.GetParentMenu();
            foreach (var item in result)
            {
                item.ChildMenus = menuService.GetChildMenu(item.MenuID);
            }
            return PartialView(result);
        }

        public ActionResult MenuList()
        {
            return View();
        }

        public JsonResult GetAllItems(KendoGridRequest request)
        {
            List<Menu> result = menuService.GetAll();
            return Json(new { data = result, total = result.Count }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult CheckNameExisted()
        {
            string name = Request["Name"];
            string id = Request["ID"];
            JsonResult result = new JsonResult();
            result.Data = menuService.CheckNameExisted(name,Guid.Parse(id));
            return result;
        }

        //
        // GET: /Menu/Create
        [HttpGet]
        public ActionResult Create()
        {
            Menu menu = new Menu();
            this.GetParentDeptList();
            return View(menu);
        }

        //
        // POST: /Menu/Create

        [HttpPost]
        public ActionResult CreateAndEdit(Menu model)
        {
            try
            {
                // TODO: Add insert logic here
                if (Request.IsAjaxRequest())
                {
                    if (model.MenuID.ToString() == PublicHelper.EmptyGuid.ToString()) //add new entity
                    {
                        model.MenuID = Guid.NewGuid();
                        menuService.Add(model);
                    }
                    else  //update entity
                    {
                        if (ModelState.IsValid)
                        {
                            menuService.Update(model);
                        }
                    }

                }
                return Json(new { data = true, message = "OK" }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet);
            }
        }

        //
        // GET: /Menu/Edit/5
        [HttpGet]
        public ActionResult Edit(string id)
        {
            this.GetParentDeptList();
            var menu = menuService.GetItemByID(id);
            return View("Create", menu);
        }

        //
        // GET: /Menu/Delete/5
        public ActionResult Delete(string id)
        {
            try
            {
                //该部门下面存在子部门不能被删除
                if (menuService.GetAll().Where(c => c.ParentMenuID.ToString() == id).Count() > 0)
                {
                    return Json(new { message = "该菜单存在子菜单不能被删除!" }, JsonRequestBehavior.AllowGet);
                }

                menuService.Delete(id);
                return Json(new { message = "ok" }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { message = "no" }, JsonRequestBehavior.AllowGet);
            }
        }

        private void GetParentDeptList()
        {
            ViewData["MenuList"] = from a in menuService.GetAll()
                                   select new SelectListItem
                                   {
                                       Text = a.Name,
                                       Value = a.MenuID.ToString()
                                   };

        }

    }
}
